package com.lwc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwc.bean.DeliveryEntryDetails;
import com.lwc.bean.MaterialStock;
import com.lwc.bean.Outbound;
import com.lwc.bean.OutboundQuery;
import com.lwc.common.bean.PageBean;
import com.lwc.common.exceotion.BusinessexCeption;
import com.lwc.mapper.MaterialStockMapper;
import com.lwc.mapper.OutboundMapper;
import com.lwc.service.OutboundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OutboundServiceImpl  implements OutboundService {
   @Autowired
    OutboundMapper outboundMapper;
    @Override
    public PageBean query(OutboundQuery outboundQuery) {
        PageHelper.startPage(outboundQuery.getPage(),outboundQuery.getPageSize());
        List<Outbound> list=outboundMapper.query(outboundQuery);
        PageInfo info=new PageInfo<>(list);

        return new PageBean<>(outboundQuery.getPage(), outboundQuery.getPageSize(), info.getTotal(),list);
    }

    @Override
    public List<DeliveryEntryDetails> getqueryById(int id) {
        return outboundMapper.getqueryById(id);
    }
@Autowired
MaterialStockMapper materialStockMapper;

    @Transactional
    @Override
    public int getupdatetrue(int id) {

      //通过出货单id 查看 出货单详情
        List<DeliveryEntryDetails> list = outboundMapper.getqueryById(id);
//获取所有的库存信息
        List<MaterialStock> goods = materialStockMapper.query(null);

        boolean is=false;
//库存
        int ids=0;
        for (DeliveryEntryDetails deliveryEntryDetails : list) {
            for (MaterialStock good : goods) {

         if (deliveryEntryDetails.getGodown_name().equals(good.getMaterial_name())){
             is=true;
             ids=good.getId();
             break;
        }else {
             is=false;
         }
            }
            if (is){
              int  stock = materialStockMapper.querystock(ids);
                //查询一下是否还有库存 是否够扣吗
                if (stock>=deliveryEntryDetails.getOut_stock()){
                    //够扣的话   扣
                    outboundMapper.deduct(ids,deliveryEntryDetails.getOut_stock());
                   // 改变审核状态
                    return outboundMapper.getupdatetrue(id);
                }else {
                    throw new BusinessexCeption("库存不足");
                }
            }{
                System.out.println("没有这个商品");
            }
        }
        return 0;
    }
}
